setTimeout(function(){
	//$('body > div > div.container > div.content > div > div.mid > div > a.seller-login > span.seller-entry.alipay').click();
	//document.getElementsByClassName('seller-entry')[0].click();
	// var xhr = new XMLHttpRequest();
	// xhr.onreadystatechange = function(){
		// if (xhr.readyState == 4) {
			// console.log(xhr);
		// }
	// }; // Implemented elsewhere.
	// xhr.open("GET", "http://24xhw.cn", true);
	// xhr.send();
	
	
	// localStorage.transfer_info = JSON.stringify({
		// money: 1,
		// fee:   0.002,
		// payee_account: 'diyuanpan8lh4@163.com',
		// pay_pwd: 'ujm65mju4',
		// interval: 5
	// });

	if(localStorage.transfer_status == '1'){
		checkTransfer();
	}
	
	if(localStorage.forward_status == '1'){
		checkPutForward();
	}
	
}, 3000);

if(sessionStorage.flush_status == '1'){
	setTimeout(function(){
		if(sessionStorage.flush_status == '1'){
			console.log(localStorage.flush_time);
			window.location.reload();
		}
	}, localStorage.flush_time * 1000);
}

function checkTransfer(){
	if(localStorage.transfer_info == undefined){
		alert('transfer_info null!');
		return true;
	}
	window.transfer_info = JSON.parse(localStorage.transfer_info);
	console.log(window.transfer_info);
	if(window.location.pathname == '/payment/transfer/index.htm'){
		if($('#J_paymentForm > div.summary.fn-clear > div > div').text() == '该账户余额支付功能关闭，不可将资金转出。'){
			alert('无法转账');
			return true;
		}
		if(!transfer(1) && localStorage.transfer_status == '1'){
			var interval = window.transfer_info.interval || 5;
			setTimeout(function(){
				window.location.reload();
			}, parseInt(Math.random() * 4) * 1000 + interval * 1000);
		}
	}else if(window.location.pathname == '/payment/transfer/check.htm'){
			var checkSafe = $('#J_transferCheckForm > div.mi-form-item.pay-password-item > div.ui-securitycore.ui-securitycore-tip.J-securitycoreTip > div > div.ui-form-explain').text();
			if(checkSafe == '安全设置检测成功！无需短信校验。'){
				if(!$('#payPassword_rsainput').is(':hidden')){
					$('#payPassword_rsainput').focus();
					setTimeout(function(){
						transfer(2);
					}, Math.random() * 2000 + 2000);
				}
			}else {
				if(checkSafe == '支付宝正在检测你的安全设置...'){
					setTimeout(checkTransfer, 1000);
				}else {
					
				}
			}
		
	}else if(window.location.pathname == '/payment/transfer/detail.htm'){
		try{
			if($('#J_transferDetailPage > div.ui-resultstep-container.fn-clear.ui-resultstep-two > ul > li.ui-resultstep-item.ui-resultstep-last.ui-resultstep-finish.ui-resultstep-current > div.ui-resultstep-steptext').text().indexOf('成功') !== false){
				setTimeout(function(){
					window.location.href = "https://bizfundprod.alipay.com/payment/transfer/index.htm";
				}, Math.random() * 3000 + 3000);
			}else {
				alert('失败');
			}
		}catch{
			alert('失败');
		}
	}else if(window.location.pathname == '/error.htm'){
		try{
			window.location.href = "https://bizfundprod.alipay.com/payment/transfer/index.htm";
		}catch{
			alert('失败');
		}
	}
}

function checkPutForward(){
	if(localStorage.forward_info == undefined){
		alert('forward_info null!');
		return true;
	}
	window.forward_info = JSON.parse(localStorage.forward_info);
	console.log(window.forward_info);
	if(window.location.pathname == '/payment/paytobankcard/index.htm'){
		if(!putForward(1) && localStorage.forward_status == '1'){
			var interval = window.forward_info.interval || 5;
			setTimeout(function(){
				window.location.reload();
			}, parseInt(Math.random() * 4) * 1000 + interval * 1000);
		}
	}else if(window.location.pathname == '/payment/paytobankcard/check.htm'){
		var checkSafe = $('#J_transferToBankCardCheckForm > div.mi-form-item.pay-password-item > div.ui-securitycore.ui-securitycore-tip.J-securitycoreTip > div > div.ui-form-explain').text();
		if(checkSafe == '安全设置检测成功！无需短信校验。'){
			if(!$('#payPassword_rsainput').is(':hidden')){
				$('#payPassword_rsainput').focus();
				setTimeout(function(){
					putForward(2);
				}, Math.random() * 2000 + 1500);
			}
		}else {
			if(checkSafe == '支付宝正在检测你的安全设置...'){
				setTimeout(checkPutForward, 1000);
			}else {
				
			}
		}
	}else if(window.location.pathname == '/payment/paytobankcard/detail.htm'){
		try{
			if($('#J_bankCardDetailPage > div.ui-resultstep-container.fn-clear.ui-resultstep-two > ul > li.ui-resultstep-item.ui-resultstep-last > div.ui-resultstep-steptext').text().indexOf('预计到账时间') !== false){
				setTimeout(function(){
					window.location.href = "https://bizfundprod.alipay.com/payment/paytobankcard/index.htm?transferType=RELATED";
				}, Math.random() * 3000 + 3000);
			}else {
				alert('失败');
			}
		}catch{
			alert('失败');
		}
	}
}

function query(){
	if(window.location.pathname == '/enterprise/tradeListQuery.htm'){
		function run(){
				setTimeout(function(){
					if(window.localStorage.run === '1'){
						$('#root > div > div > div > div.transitionTable___93dNL > div.tableHeadWrapper___M6pdf > div > div.ant-tabs-bar > div > div > div > div > div:nth-child(8)').click();
						var td = $('#root > div > div > div > div.transitionTable___93dNL > div:nth-child(2) > div > div > div > div > div > div.ant-table-scroll > div > table > tbody > tr').find('td');
						var data = {};
						data.date   = td.eq(0).text();
						data.remark = td.eq(1).find('div > div:nth-child(1)').text();
						data.trade_no = td.eq(2).find('div > span > span').attr('title');
						data.userid = td.eq(3).find('div > div:nth-child(1)').text();
						data.money  = td.eq(4).find('div > span > span').text();
						
						console.log(data);
						$.post('http://127.0.0.1/test/notify.php', data);
						// console.log($('#root > div > div > div > div.transitionTable___93dNL > div:nth-child(2) > div > div > div > div > div > div.ant-table-scroll > div > table > tbody > tr > td:nth-child(2) > div > div:nth-child(1)').text());
						// console.log($('#root > div > div > div > div.transitionTable___93dNL > div:nth-child(2) > div > div > div > div > div > div.ant-table-scroll > div > table > tbody > tr > td:nth-child(3) > div > span > span').attr('title'));
						// console.log($('#root > div > div > div > div.transitionTable___93dNL > div:nth-child(2) > div > div > div > div > div > div.ant-table-scroll > div > table > tbody > tr > td:nth-child(4) > div > div:nth-child(1)').text());
						// console.log($('#root > div > div > div > div.transitionTable___93dNL > div:nth-child(2) > div > div > div > div > div > div.ant-table-scroll > div > table > tbody > tr > td:nth-child(5) > div > span > span').text());
						// console.log($('#root > div > div > div > div.transitionTable___93dNL > div:nth-child(2) > div > div > div > div > div > div.ant-table-scroll > div > table > tbody > tr > td:nth-child(8) > span > span').text());
					}
					run();
				}, 4500);
		}
		run();
	}
}

function transfer(step){
	var transfer_info = window.transfer_info;
	switch(step){
		case 1:
			var balance = $('#J_paymentForm > div.summary.fn-clear > div > span > em > span > span').text().replace(/,/g, '');
			if(transfer_info.money == undefined || balance <= transfer_info.money ){
				console.log(balance + ' < ' + transfer_info.money);
				return false;
			}
			var fee = transfer_info.fee * balance;
			if(fee < 2){
				fee = 2;
			}
			var money = Math.floor(balance - fee);
			
			// var oEvent = document.createEvent("MouseEvents");
			// oEvent.initMouseEvent("click", true, true, document.defaultView, 0, 810 + parseInt(Math.random()*20), 250 + parseInt(Math.random()*30), 810 + parseInt(Math.random()*20), 250+ parseInt(Math.random()*30)); 
			// $('#J_paymentForm > div.mi-form-item.fn-mt30')[0].dispatchEvent(oEvent);
			setTimeout(function(){
				$('#J_payeeShowAccount').focus();
				//$('#J_payeeShowAccount').blur();
				setTimeout(function(){
					$('#J_payeeShowAccount').val(transfer_info.payee_account);
					setTimeout(function(){
						$('#J_transferAmount').focus();
						setTimeout(function(){
							$('#J_transferAmount').val(money);
							setTimeout(function(){
								$('#J_transferAmount').blur();
								setTimeout(function(){
									//$('#J_transferAmount').blur();
									document.getElementById('J_paymentForm').submit();
									setTimeout(function(){
										window.location.reload();
									}, 2000);
								}, Math.random() * 2000 + 1000);
							}, Math.random() * 1000 + 1000);
						}, Math.random() * 1000 + 1000);
					}, Math.random() * 500 + 500);
				}, Math.random() * 2000 + 2000);
			}, Math.random() * 500 + 1000);
			return true;
		case 2:
			$('#payPassword_rsainput').val(transfer_info.pay_pwd);
			setTimeout(function(){
				$('#smsSendChk').click();
			}, Math.random() * 1000 + 1500);
			return true;
	}
}

function putForward(step){
	var forward_info = window.forward_info;
	switch(step){
		case 1:
			var balance = $('#J_paymentToBankCardForm > div.summary.fn-clear > div > span > em > span > em').text().replace(/,/g, '');
			if(forward_info.money == undefined || balance <= forward_info.money ){
				console.log(balance + ' < ' + forward_info.money);
				return false;
			}
			var fee = forward_info.fee * balance;
			if(fee < 2){
				fee = 2;
			}
			var money = Math.floor(balance - fee);
			setTimeout(function(){
				$('#J_paymentToBankCardAmount').focus();
				setTimeout(function(){
					$('#J_paymentToBankCardAmount').val(money);
					setTimeout(function(){
						$('#J_paymentToBankCardRemark').focus();
						setTimeout(function(){
							$('#J_paymentToBankCardRemark').val(money);
							setTimeout(function(){
								$('#J_currentDay').click();
								setTimeout(function(){
									$('#J_formSubmitButton').click();
								}, Math.random() * 500 + 2500);
							}, Math.random() * 500 + 1300);
						}, Math.random() * 500 + 1300);
					}, Math.random() * 500 + 1300);
				}, Math.random() * 500 + 1000);
			}, Math.random() * 500);
			return true;
		case 2: 
			$('#payPassword_rsainput').val(forward_info.pay_pwd);
			setTimeout(function(){
				$('#smsSendChk').click();
			}, Math.random() * 1000 + 1500);
			return true;
	}
}

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
	switch(request.type) {
		case 'transfer':
			if (request.status) {
				console.log('开启转账')
				localStorage.transfer_status = '1';
			} else {
				localStorage.transfer_status = '0';
				console.log('停止转帐')
			}
			break;
		case 'forward':
			if (request.status) {
				console.log('开启提现')
				localStorage.forward_status = '1';
			} else {
				console.log('停止提现')
				localStorage.forward_status = '0';
			}
			break;
		case 'flush':
			if (request.status) {
				console.log('开启刷新')
				sessionStorage.flush_status = '1';
			} else {
				console.log('停止刷新')
				sessionStorage.flush_status = '0';
			}
			break;
	}
})

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
	console.log(request);
	if(request.type == 'transfer'){
		if(request.status == true){
			localStorage.transfer_status = '1';
			if(window.location.pathname == "/payment/transfer/index.htm"){
				window.location.reload();
			}else {
				window.location.href = "https://bizfundprod.alipay.com/payment/transfer/index.htm";
			}
		}else {
			localStorage.transfer_status = '0';
		}
	}else if(request.type == 'flush'){
		if(sessionStorage.flush_status == undefined || sessionStorage.flush_status == '0'){
			sessionStorage.flush_status = '1';
			localStorage.flush_time = prompt('刷新间隔', '30') || 30;
			window.location.reload();
		}else {
			sessionStorage.flush_status = '0';
		}
	}else if(request.type == 'forward'){
		if(request.status == true){
			localStorage.forward_status = '1';
			if(window.location.pathname == "/payment/paytobankcard/index.htm"){
				window.location.reload();
			}else {
				window.location.href = "https://bizfundprod.alipay.com/payment/paytobankcard/index.htm?transferType=RELATED";
			}
		}else {
			localStorage.forward_status = '0';
		}
	}
});